package tree;
//https://leetcode.cn/leetbook/read/illustration-of-algorithm/7ffx6t/
//某公司组织架构以二叉搜索树形式记录，节点值为处于该职位的员工编号。请返回第 cnt 大的员工编号。
public class 寻找二叉搜索树中的目标节点 {
      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    class Solution {
        int res, cnt;
        public int findTargetNode(TreeNode root, int cnt) {
            this.cnt = cnt;
            dfs(root);
            return res;
        }
        void dfs(TreeNode root) {
            if(root == null) return;
            dfs(root.right);
            if(cnt == 0) return;
            if(--cnt == 0) res = root.val;
            dfs(root.left);
        }
    }
}
